#include "GamePlayScene.h"
#include "Utils.h"

USING_NS_CC;

// Type of the game
char* TypeLeft_miniGame[] =
{
	"JustMath",
	"Operations",
	"RPS",
	"Sumof12",
	"Whereisit",
	""
};
char* TypeRight_miniGame[] =
{
	"Color",
	"Concentration",
	"LeftRight",
	"Matching",
	"Runner",
	""
};
GamePlayScene::GamePlayScene() :iAnimLeftRunning(true),
iAnimRightRunning(true), s_LifeCount(KMAX_LIFE), indexTypeLeft(0), indexTypeRight(0),
pointLeft(100), pointRight(100), PointSum(0), startTime(1)
{	
	// Anim timer
	animTimers = new Armature*[2];
	// Type mini game
	UILeft = new Layout*[KTYPE_MINIGAME];
	UIRight = new Layout*[KTYPE_MINIGAME];

	ArrGameTypeLeft = Utils::getArrRandomFolder(KTYPE_MINIGAME);
	ArrGameTypeRight = Utils::getArrRandomFolder(KTYPE_MINIGAME);  
}
GamePlayScene::~GamePlayScene()
{

}

Scene* GamePlayScene::createScene()
{
    auto scene = Scene::create();   
    auto layer = GamePlayScene::create();
	scene->addChild(layer);
    return scene;
}

bool GamePlayScene::init()
{
    if ( !Layer::init() )  return false;  
	
	// Load game scene
	addBackgroundScene();
	// Show question left and right
	getRanMiniGameLeft();
	getRanMiniGameRight();


	this->scheduleUpdate();
    return true;
}
void GamePlayScene::addBackgroundScene()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	Node *gameScene = SceneReader::getInstance()->createNodeWithSceneFile("publish/GameScene.json");
#else
	Node *gameScene = SceneReader::getInstance()->createNodeWithSceneFile("GameScene.json");
#endif
	this->addChild(gameScene, ZINDEX_BACKGROUND);

	// Get anim timers	
	ComRender *renderLeft = (ComRender*)(gameScene->getChildByTag(10009)->getComponent("CCArmature"));
	animTimers[0] = (Armature*)(renderLeft->getNode());

	ComRender *renderRight = (ComRender*)(gameScene->getChildByTag(10010)->getComponent("CCArmature"));
	animTimers[1] = (Armature*)(renderRight->getNode());

	// Load lifes
	lifeArray = new Sprite*[KMAX_LIFE];
	for (int i = 0; i < KMAX_LIFE; i++){
		ComRender *renderLife =  (ComRender*)(gameScene->getChildByTag(10004 + i)->getComponent("CCSprite"));
		lifeArray[i] = (Sprite*)(renderLife->getNode());
	}

	// Visible UI left
	int CountIndex = 10011 + getArrNumberType(TypeLeft_miniGame);
	int index = 0;
	for(int indexTagLeft = 10011; indexTagLeft < CountIndex; indexTagLeft++)
	{
		ComRender* renderUILeft = (ComRender*)(gameScene->getChildByTag(indexTagLeft)->getComponent("GUIComponent"));
		UILeft[index] = (Layout*)(renderUILeft->getNode());
		UILeft[index]->setVisible(false);
		index++;
		//log("indexTagLeft:%d",indexTagLeft);
	}

	// Visible UI right
	index = 0;
	int CountIndexRight = 10016 + getArrNumberType(TypeRight_miniGame);
	for(int indexTagRight = 10016; indexTagRight < CountIndexRight; indexTagRight++)
	{
		ComRender* renderUIRight = (ComRender*)(gameScene->getChildByTag(indexTagRight)->getComponent("GUIComponent"));
		UIRight[index] = (Layout*)(renderUIRight->getNode());
		UIRight[index]->setVisible(false);
		index++;
		//log("indexTagRight:%d",indexTagRight);
	}

	// Get HUD text
	ComRender *renderHud = (ComRender*)(gameScene->getChildByTag(10021)->getComponent("GUIComponent"));
	Layout *layerHud = (Layout*)(renderHud->getNode());
	txt_NumberScore = (Text*)(layerHud->getChildByName("num_score"));
	txt_NumberScore->setColor(ccColor3B::BLUE);
	txt_NumberScoreLeft = (Text*)(layerHud->getChildByName("num_Left"));
	txt_NumberScoreRigth = (Text*)(layerHud->getChildByName("num_Right"));

	txt_NumberScore->setString("0");
	txt_NumberScoreLeft->setString("100");
	txt_NumberScoreRigth->setString("100");
}
void GamePlayScene::update(float dt)
{
	startTime++;
	// For timer on left
	if (!animTimers[0]->getAnimation()->isPlaying() && iAnimLeftRunning)
	{
		//log("[Update timer left not running!]");	
		s_LifeCount--;
		//log("s_LifeCount_Left:%d",s_LifeCount);
		iAnimLeftRunning = false;			
		if(s_LifeCount >= 0)
		{
			lifeArray[s_LifeCount]->setVisible(false);
			if(s_LifeCount > 0)			
				playAnimTimerLeft();
		}		
	}
	else if(!iAnimLeftRunning)
	{
		animTimers[0]->setOpacity(128);	 		
	}
	else
	{
		//log("Timer left running and minus point");	
		if(startTime == KFPS_TIMER)
		{
			pointLeft -= 10;
			__String *scoreLeft = __String::createWithFormat("%d",pointLeft);
			txt_NumberScoreLeft->setString(scoreLeft->getCString());
		}  	
		
	}
	// For timer on right
	if (!animTimers[1]->getAnimation()->isPlaying() && iAnimRightRunning)
	{
		//log("[Update timer right not running!]");
		s_LifeCount--;
		//log("s_LifeCount_Right:%d",s_LifeCount);
		iAnimRightRunning = false;			
		if(s_LifeCount >= 1)
		{
			lifeArray[s_LifeCount]->setVisible(false);	
			if(s_LifeCount > 1)				
				playAnimTimerRight();			
		}			
	}
	else if(!iAnimRightRunning)
	{
		animTimers[1]->setOpacity(128);
	}
	else
	{
		//log("Timer right running and minus point");
		if(startTime == KFPS_TIMER)
		{
			pointRight -= 10;
			__String *scoreRight = __String::createWithFormat("%d",pointRight);
			txt_NumberScoreRigth->setString(scoreRight->getCString());
		}

	}
	if(startTime == KFPS_TIMER)
	{
		startTime = 1;
	}
	if(s_LifeCount == 0)
	{
		gotoGameOver();
	}
}
void GamePlayScene::gotoGameOver()
{
	Director::getInstance()->replaceScene(GameOverScene::createScene());
}
void GamePlayScene::playAnimTimerLeft()
{
	iAnimLeftRunning = true;
	animTimers[0]->getAnimation()->playByIndex(Anim_LeftTimer);
}
void GamePlayScene::playAnimTimerRight()
{
	iAnimRightRunning = true;
	animTimers[1]->getAnimation()->playByIndex(Anim_RightTimer);
}
int GamePlayScene::getArrNumberType(char* arr[])
{
   int i = 0;
	while(arr[i] != "")
	{
		i++;
	}
	return i;
}
void GamePlayScene::getRanMiniGameLeft()
{
	if (ArrGameTypeLeft.size() == 0) return;
	for( int i = 0; i < ArrGameTypeLeft.size(); i ++ )
	{
		indexTypeLeft = ArrGameTypeLeft[i];
		ArrGameTypeLeft.erase(ArrGameTypeLeft.begin() + i);
		break;
	}
	UILeft[indexTypeLeft]->setVisible(true);
	updateQuestionLeft();
}
void GamePlayScene::getRanMiniGameRight()
{
	if (ArrGameTypeRight.size() == 0) return;
	/*for (int i = 0; i < ArrGameTypeRight.size(); i++)
	{
		indexTypeRight = ArrGameTypeRight[i];
		
		ArrGameTypeRight.erase(ArrGameTypeRight.begin() + i);
		break;
	}*/
	indexTypeRight = 1;
	log("Random type mini game on right____________:%d", indexTypeRight);
	UIRight[indexTypeRight]->setVisible(true);
	updateQuestionRight();
}
void GamePlayScene::updateQuestionLeft()
{
	switch (indexTypeLeft)
	{
	case JUSTMATH:
		{			
			ArrGameFolderLeft = Utils::getArrRandomFolder(NUMBERGAME_JUSTMATCH);
			justmatch_Paint();
		}
		break;

	case OPERATIONS:
		{			
			ArrGameFolderLeft = Utils::getArrRandomFolder(NUMBERGAME_OPERATIONS);			
			operations_Paint();
		}
		break;

	case RPS:
		{			
			ArrGameFolderLeft = Utils::getArrRandomFolder(NUMBERGAME_RPS);	  			
			RPS_Paint();
		}
		break;

	case SUMOF12:
		{			
			ArrGameFolderLeft = Utils::getArrRandomFolder(NUMBERGAME_SUMOF12);			
			sumof12_Paint();
		}
		break;

	case WHEREISIT:
		{			
			ArrGameFolderLeft = Utils::getArrRandomFolder(NUMBERGAME_WHEREISIT);			
			Whereisit_Paint();
		}
		break;
	default:
		break;
	}
}
void GamePlayScene::justmatch_Paint()
{
	log("Justmatch_Painting...");	
	auto txt_title = (Text*)(UILeft[indexTypeLeft]->getChildByName("txt_tittle"));	
	auto img   = (ImageView*)(UILeft[indexTypeLeft]->getChildByName("img_puzzle"));	
	txt_title->setText("Ket qua cua phep tinh nay?");
	txt_title->setFontSize(18);
	std::string strFolder, urlFolder;
	for (int i = 0; i < ArrGameFolderLeft.size(); i++)
	{
		strFolder = chageInt2String(ArrGameFolderLeft[i]);
		ArrGameFolderLeft.erase(ArrGameFolderLeft.begin() + i);
		break;
	}
	__String *strimg = __String::createWithFormat("img_%s.png", strFolder.c_str());
	std::string url  = "q_left/";
	url.append(TypeLeft_miniGame[indexTypeLeft]);
	url.append("/");
	url.append(strFolder);
	url.append("/");
	urlFolder = url;
	url.append(strimg->getCString());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	img->loadTexture(url);
#else
	img->loadTexture(strimg);
#endif	
	__String* numberBtn, *imgBtn;
	std::string urlImgBtn;
	urlImgBtn = urlFolder;
	int *ArrRandom = Utils::getArrRandomNumber(3);
	int numberFile;
	for (int i = 0; i < 3; i++)
	{
		numberFile = ArrRandom[i] + 1;
		numberBtn  = __String::createWithFormat("btn_%d", i+1);
		imgBtn	   = __String::createWithFormat("as_%d.png", numberFile);
		urlImgBtn.append(imgBtn->getCString());
		log("urlImgBtn:%s", urlImgBtn.c_str());	
		auto btn = (Button*)(UILeft[indexTypeLeft]->getChildByName(numberBtn->getCString()));
		if (numberFile == 1)
			btn->setTag(KBUTTONWIN_TAG);
		else
			btn->setTag(KBUTTONWRONG_TAG);
		btn->loadTextureNormal(urlImgBtn);
		btn->addTouchEventListener(this, toucheventselector(GamePlayScene::pressButtonWinLeft));
		urlImgBtn = urlFolder;
	}
}
void GamePlayScene::operations_Paint()
{
	log("operations_Painting...");
	auto txt_title = (Text*)(UILeft[indexTypeLeft]->getChildByName("txt_tittle"));	
	auto img       = (ImageView*)(UILeft[indexTypeLeft]->getChildByName("img_puzzle"));	
	txt_title->setText("Ket qua cua phep tinh nay?");
	txt_title->setFontSize(18);	
	std::string strFolder, urlFolder;
	for (int i = 0; i < ArrGameFolderLeft.size(); i++)
	{
		strFolder = chageInt2String(ArrGameFolderLeft[i]);
		ArrGameFolderLeft.erase(ArrGameFolderLeft.begin() + i);
		break;
	}
	__String *strimg = __String::createWithFormat("img_%s.png", strFolder.c_str());
	std::string url  = "q_left/";
	url.append(TypeLeft_miniGame[indexTypeLeft]);
	url.append("/");
	url.append(strFolder);
	url.append("/");
	urlFolder = url;
	url.append(strimg->getCString());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	img->loadTexture(url);
#else
	img->loadTexture(strimg);
#endif
	// Load button answer
	__String* numberBtn, *imgBtn;
	std::string urlImgBtn;
	urlImgBtn = urlFolder;
	int *ArrRandom = Utils::getArrRandomNumber(3);
	int numberFile;
	for (int i = 0; i < 3; i++)
	{
		numberFile = ArrRandom[i] + 1;
		numberBtn  = __String::createWithFormat("btn_%d", i+1);
		imgBtn	   = __String::createWithFormat("as_%d.png", numberFile);
		urlImgBtn.append(imgBtn->getCString());
		log("urlImgBtn:%s", urlImgBtn.c_str());
		auto btn = (Button*)(UILeft[indexTypeLeft]->getChildByName(numberBtn->getCString()));
		if (numberFile == 1)
			btn->setTag(KBUTTONWIN_TAG);
		else
			btn->setTag(KBUTTONWRONG_TAG);
		btn->loadTextureNormal(urlImgBtn);
		btn->addTouchEventListener(this, toucheventselector(GamePlayScene::pressButtonWinLeft));
		urlImgBtn = urlFolder;
	}
}
void GamePlayScene::RPS_Paint()
{
	log("RPS_Painting...");
	auto txt_title = (Text*)(UILeft[indexTypeLeft]->getChildByName("txt_tittle"));	
	auto img   = (ImageView*)(UILeft[indexTypeLeft]->getChildByName("img_puzzle"));	
	txt_title->setText("Ket qua cua phep tinh nay?");
	txt_title->setFontSize(18);		
	std::string strFolder, urlFolder;
	for (int i = 0; i < ArrGameFolderLeft.size(); i++)
	{
		strFolder = chageInt2String(ArrGameFolderLeft[i]);
		ArrGameFolderLeft.erase(ArrGameFolderLeft.begin() + i);
		break;
	}
	__String *strimg = __String::createWithFormat("img_%s.png", strFolder.c_str());
	std::string url  = "q_left/";
	url.append(TypeLeft_miniGame[indexTypeLeft]);
	url.append("/");
	url.append(strFolder);
	url.append("/");
	urlFolder = url;
	url.append(strimg->getCString());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	img->loadTexture(url);
#else
	img->loadTexture(strimg);
#endif
	__String* numberBtn, *imgBtn;
	std::string urlImgBtn;
	urlImgBtn = urlFolder;
	int *ArrRandom = Utils::getArrRandomNumber(3);
	int numberFile;
	for (int i = 0; i < 3; i++)
	{
		numberFile = ArrRandom[i] + 1;
		numberBtn  = __String::createWithFormat("btn_%d", i+1);
		imgBtn	   = __String::createWithFormat("as_%d.png", numberFile);
		urlImgBtn.append(imgBtn->getCString());
		log("urlImgBtn:%s", urlImgBtn.c_str());	
		auto btn = (Button*)(UILeft[indexTypeLeft]->getChildByName(numberBtn->getCString()));
		if (numberFile == 1)
			btn->setTag(KBUTTONWIN_TAG);
		else
			btn->setTag(KBUTTONWRONG_TAG);
		btn->loadTextureNormal(urlImgBtn);
		btn->addTouchEventListener(this, toucheventselector(GamePlayScene::pressButtonWinLeft));
		urlImgBtn = urlFolder;
	}
}
void GamePlayScene::sumof12_Paint()
{
	log("sumof12_Painting...");
	auto txt_title = (Text*)(UILeft[indexTypeLeft]->getChildByName("txt_tittle"));	
	auto img       = (ImageView*)(UILeft[indexTypeLeft]->getChildByName("img_puzzle"));	
	txt_title->setText("Ket qua cua phep tinh nay?");
	txt_title->setFontSize(18);		
	std::string strFolder, urlFolder;
	for (int i = 0; i < ArrGameFolderLeft.size(); i++)
	{
		strFolder = chageInt2String(ArrGameFolderLeft[i]);
		ArrGameFolderLeft.erase(ArrGameFolderLeft.begin() + i);
		break;
	}
	__String *strimg = __String::createWithFormat("img_%s.png", strFolder.c_str());
	std::string url  = "q_left/";
	url.append(TypeLeft_miniGame[indexTypeLeft]);
	url.append("/");
	url.append(strFolder);
	url.append("/");
	urlFolder = url;
	url.append(strimg->getCString());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	img->loadTexture(url);
#else
	img->loadTexture(strimg);
#endif	
	__String* numberBtn, *imgBtn;
	std::string urlImgBtn;
	urlImgBtn = urlFolder;
	int *ArrRandom = Utils::getArrRandomNumber(3);
	int numberFile;
	for (int i = 0; i < 3; i++)
	{
		numberFile = ArrRandom[i] + 1;
		numberBtn  = __String::createWithFormat("btn_%d", i+1);
		imgBtn	   = __String::createWithFormat("as_%d.png", numberFile);
		urlImgBtn.append(imgBtn->getCString());
		log("urlImgBtn:%s", urlImgBtn.c_str());	
		auto btn = (Button*)(UILeft[indexTypeLeft]->getChildByName(numberBtn->getCString()));
		if (numberFile == 1)
			btn->setTag(KBUTTONWIN_TAG);
		else
			btn->setTag(KBUTTONWRONG_TAG);
		btn->loadTextureNormal(urlImgBtn);
		btn->addTouchEventListener(this, toucheventselector(GamePlayScene::pressButtonWinLeft));
		urlImgBtn = urlFolder;
	}
}
void GamePlayScene::Whereisit_Paint()
{
	log("Whereisit_Painting...");
	auto txt_title = (Text*)(UILeft[indexTypeLeft]->getChildByName("txt_tittle"));
	auto img = (ImageView*)(UILeft[indexTypeLeft]->getChildByName("img_puzzle"));
	txt_title->setText("Ket qua cua phep tinh nay?");
	txt_title->setFontSize(18);
	std::string strFolder, urlFolder;
	for (int i = 0; i < ArrGameFolderLeft.size(); i++)
	{			
		strFolder = chageInt2String(ArrGameFolderLeft[i]);
		ArrGameFolderLeft.erase(ArrGameFolderLeft.begin() + i);
		break;
	}		
	__String *strimg = __String::createWithFormat("img_%s.png", strFolder.c_str());
	std::string url = "q_left/";
	url.append(TypeLeft_miniGame[indexTypeLeft]);
	url.append("/");
	url.append(strFolder);
	url.append("/");
	urlFolder = url;
	url.append(strimg->getCString());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	img->loadTexture(url);
#else
	img->loadTexture(strimg);
#endif
	// Load button answer
	__String* numberBtn, *imgBtn;
	std::string urlImgBtn;
	urlImgBtn = urlFolder;
	int *ArrRandom = Utils::getArrRandomNumber(3);
	int numberFile;
	for (int i = 0; i < 3; i++)
	{
		numberFile = ArrRandom[i] + 1;
		numberBtn  = __String::createWithFormat("btn_%d", i+1);
		imgBtn	   = __String::createWithFormat("as_%d.png", numberFile);
		urlImgBtn.append(imgBtn->getCString());
		log("urlImgBtn:%s", urlImgBtn.c_str());	
		auto btn = (Button*)(UILeft[indexTypeLeft]->getChildByName(numberBtn->getCString()));
		if (numberFile == 1)
			btn->setTag(KBUTTONWIN_TAG);
		else
			btn->setTag(KBUTTONWRONG_TAG);
		btn->loadTextureNormal(urlImgBtn);
		btn->addTouchEventListener(this, toucheventselector(GamePlayScene::pressButtonWinLeft));
		urlImgBtn = urlFolder;
	}
}
std::string GamePlayScene::chageInt2String(int i)
{
	switch (i)
	{
		case 0:
			return "1";
			break;
		case 1:
			return "2";
			break;
		case 2:
			return "3";
			break;
		case 3:
			return "4";
			break;
		case 4:
			return "5";
			break;
		case 5:
			return "6";
			break;
		case 6:
			return "7";
			break;
		case 7:
			return "8";
			break;
		case 8:
			return "9";
			break;
		case 9:
			return "10";
			break;
		default:
			break;
	}
}
bool GamePlayScene::checkGameFolder(std::vector<std::string> F_left, std::string indexFolder)
{
	if (F_left.size() == 0)	return false;
	for (int i = 0; i < F_left.size(); i++)
	{
		std::string str =  F_left.at(i).c_str();
		log("-----str-----:%s",str.c_str());
		if(str.compare(indexFolder)) return true;
	}
	return false;
}
void GamePlayScene::pressButtonWinLeft(Ref* pSender, TouchEventType type)
{
	if (type == TouchEventType::TOUCH_EVENT_ENDED)
	{
		auto btn = (Button*)pSender;
		Sprite* imgResult;	 		
		if (btn->getTag() == KBUTTONWIN_TAG)
		{
			//MessageBox("You Win!","Info");
			#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
			imgResult = Sprite::create("gfx/GUI/img_correct.png");
			imgResult->setPosition(Vec2(UILeft[indexTypeLeft]->getContentSize().width / 2, UILeft[indexTypeLeft]->getContentSize().height / 2));
			imgResult->setTag(KIMGRESULT_TAG);
			UILeft[indexTypeLeft]->addChild(imgResult);
			#else
			imgResult = Sprite::create("img_correct.png");
			imgResult->setPosition(Vec2(UILeft[indexTypeLeft]->getContentSize().width/2,UILeft[indexTypeLeft]->getContentSize().height/2));
			imgResult->setTag(KIMGRESULT_TAG);
			UILeft[indexTypeLeft]->addChild(imgResult);
			#endif
			// Sum point
			PointSum += pointLeft;	   
			__String *scoreTotal = __String::createWithFormat("%d",PointSum);
			txt_NumberScore->setString(scoreTotal->getCString());
		}
		else
		{
			//MessageBox("You Wrong!", "Info");
			#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
			imgResult = Sprite::create("gfx/GUI/img_incorrect.png");
			imgResult->setPosition(Vec2(UILeft[indexTypeLeft]->getContentSize().width/2,UILeft[indexTypeLeft]->getContentSize().height/2));
			imgResult->setTag(KIMGRESULT_TAG);
			UILeft[indexTypeLeft]->addChild(imgResult);
			#else
			imgResult = Sprite::create("img_incorrect.png");
			imgResult->setPosition(Vec2(UILeft[indexTypeLeft]->getContentSize().width / 2, UILeft[indexTypeLeft]->getContentSize().height / 2));
			imgResult->setTag(KIMGRESULT_TAG);
			UILeft[indexTypeLeft]->addChild(imgResult);
			#endif			 			
			// minus life
			s_LifeCount--;
			lifeArray[s_LifeCount]->setVisible(false);
			if (s_LifeCount > 0)
				playAnimTimerLeft();
		}
		UILeft[indexTypeLeft]->setEnabled(false);
		if( ArrGameFolderLeft.size() == 0 )			
		{
			UILeft[indexTypeLeft]->setVisible(false);
			getRanMiniGameLeft();
		}		
		else
		{
			this->scheduleOnce(schedule_selector(GamePlayScene::resetLayerLeft), 0.2);
		}
	}	   
}
void GamePlayScene::resetLayerLeft(float dt)
{
	pointLeft = 100;
	txt_NumberScoreLeft->setString("100");
	auto imgResult = (Sprite*)(UILeft[indexTypeLeft]->getChildByTag(KIMGRESULT_TAG)); 
	imgResult->removeFromParent();		 	
	UILeft[indexTypeLeft]->setEnabled(true);  	
	
	switch (indexTypeLeft)
	{
		case JUSTMATH:
			justmatch_Paint();
			break;

		case OPERATIONS:		
			operations_Paint();		
			break;

		case RPS:		
			RPS_Paint();		
			break;

		case SUMOF12:
			sumof12_Paint();
			break;

		case WHEREISIT:		
			Whereisit_Paint();		
			break;
		default:
			break;
	}
}
// process on right
void GamePlayScene::updateQuestionRight()
{
	switch (indexTypeRight)
	{
		case COLOR:
			{
				ArrGameFolderRight = Utils::getArrRandomFolder(NUMBERGAME_COLOR);
				color_Paint();
			}
			break;

		case CONCENTRATION:
			{
				ArrGameFolderRight = Utils::getArrRandomFolder(NUMBERGAME_CONCENTRATION);
				concentration_Paint();
			}
			break;

		case LEFTRIGHT:
			{
				ArrGameFolderRight = Utils::getArrRandomFolder(NUMBERGAME_LEFTRIGHT);
				leftRight_Paint();
			}
			break;

		case MATCHING:
			{
				ArrGameFolderRight = Utils::getArrRandomFolder(NUMBERGAME_MATCHING);
				matching_Paint();
			}
			break;

		case RUNNER:
			{
				ArrGameFolderRight = Utils::getArrRandomFolder(NUMBERGAME_RUNNER);
				runner_Paint();
			}
			break;
		default:
			break;
	}
}
void GamePlayScene::color_Paint()
{
	log("Color_Painting...");
	auto txt_title = (Text*)(UIRight[indexTypeRight]->getChildByName("txt_tittle"));
	txt_title->setText("select the color that doesn't belong?");
	txt_title->setFontSize(18);
	std::string strFolder;
	// Get folder of mini game
	for (int i = 0; i < ArrGameFolderRight.size(); i++)
	{
		strFolder = chageInt2String(ArrGameFolderRight[i]);
		ArrGameFolderRight.erase(ArrGameFolderRight.begin() + i);
		break;
	}
	std::string url = "q_right/";
	url.append(TypeRight_miniGame[indexTypeRight]);
	url.append("/");
	url.append(strFolder);
	url.append("/");  	
	__String* numberBtn, *imgBtn;
	std::string urlImgBtn;
	urlImgBtn = url;
	int *ArrRandom = Utils::getArrRandomNumber(6);
	int numberFile;
	for (int i = 0; i < 6; i++)
	{
		numberFile = ArrRandom[i] + 1;
		numberBtn = __String::createWithFormat("btn_%d", i + 1);
		imgBtn = __String::createWithFormat("as_%d.png", numberFile);
		urlImgBtn.append(imgBtn->getCString());
		log("urlImgBtn:%s", urlImgBtn.c_str());
		auto btn = (Button*)(UIRight[indexTypeRight]->getChildByName(numberBtn->getCString()));
		if (numberFile == 1)
			btn->setTag(KBUTTONWIN_RIGHT_TAG);
		else
			btn->setTag(KBUTTONWRONG_RIGHT_TAG);
		btn->loadTextureNormal(urlImgBtn);
		btn->addTouchEventListener(this, toucheventselector(GamePlayScene::pressButtonWinRight));
		urlImgBtn = url;
	}
}
void GamePlayScene::concentration_Paint()
{
	log("Concentration_Painting...");
	auto btn_7 = (Button*)(UIRight[indexTypeRight]->getChildByName("btn_7"));
	auto btn_8 = (Button*)(UIRight[indexTypeRight]->getChildByName("btn_8"));
	auto btn_9 = (Button*)(UIRight[indexTypeRight]->getChildByName("btn_9"));
	btn_7->setVisible(true);
	btn_8->setVisible(true);
	btn_9->setVisible(true);

	auto txt_title = (Text*)(UIRight[indexTypeRight]->getChildByName("txt_tittle"));
	txt_title->setText("Watch and remember then find a apair");
	txt_title->setFontSize(18);
	std::string strFolder;
	int folderSelected;
	// Get folder of mini game
	for (int i = 0; i < ArrGameFolderRight.size(); i++)
	{
		folderSelected = ArrGameFolderRight[i] + 1;
		strFolder = chageInt2String(ArrGameFolderRight[i]);
		ArrGameFolderRight.erase(ArrGameFolderRight.begin() + i);
		break;
	}
	std::string url = "q_right/";
	url.append(TypeRight_miniGame[indexTypeRight]);
	url.append("/");
	url.append(strFolder);
	url.append("/");
	__String* numberBtn, *imgBtn;
	std::string urlImgBtn;
	urlImgBtn = url;
	vector<string> ArrRandom;
	int numberFile;
	buttonApp *btn = new buttonApp();
	if (folderSelected <= 3)
	{
		ArrRandom = Utils::getArrRandomNumberImg(6);
		for (int i = 0; i < 6; i++)
		{
			numberBtn = __String::createWithFormat("btn_%d", i+1);
			urlImgBtn.append(ArrRandom[i].c_str());
			log("urlImgBtn:%s", urlImgBtn.c_str());
			btn = (buttonApp*)(UIRight[indexTypeRight]->getChildByName(numberBtn->getCString())); 			
			btn->loadTextureNormal(urlImgBtn);
			btn->setTextContentImg(ArrRandom[i]);
			btn->addTouchEventListener(this, toucheventselector(GamePlayScene::checkConcentration));
			urlImgBtn = url;
		}
		// visible btn not use			
		btn_7->setVisible(false);
		btn_8->setVisible(false);
		btn_9->setVisible(false);
		//auto bt = Button::create();
		//bt->getTitleText();
	}
	else
	{
		ArrRandom = Utils::getArrRandomNumberImg(8);
		for (int i = 0; i < 8; i++)
		{
			if ((i + 1) == 8)
			{
				auto btn_8 = (Button*)(UIRight[indexTypeRight]->getChildByName("btn_8"));
				btn_8->setVisible(false);
				numberBtn = __String::createWithFormat("btn_%d", i + 2);
			}
			else
			{
				numberBtn = __String::createWithFormat("btn_%d", i + 1);
			} 			
			urlImgBtn.append(ArrRandom[i].c_str());
			log("urlImgBtn:%s", urlImgBtn.c_str());
			btn = (buttonApp*)(UIRight[indexTypeRight]->getChildByName(numberBtn->getCString()));			
			btn->loadTextureNormal(urlImgBtn);
			btn->setTextContentImg(ArrRandom[i]);
			btn->addTouchEventListener(this, toucheventselector(GamePlayScene::checkConcentration));
			urlImgBtn = url;
		}
	}
	
}
void GamePlayScene::leftRight_Paint()
{
	log("Left right_Painting...");
	auto txt_title = (Text*)(UIRight[indexTypeRight]->getChildByName("txt_tittle"));
	auto img = (ImageView*)(UIRight[indexTypeRight]->getChildByName("img_puzzle"));
	txt_title->setText("Define it is left or right hand");
	txt_title->setFontSize(18);
	std::string strFolder, urlFolder;
	// Get folder of mini game
	for (int i = 0; i < ArrGameFolderRight.size(); i++)
	{
		strFolder = chageInt2String(ArrGameFolderRight[i]);
		ArrGameFolderRight.erase(ArrGameFolderRight.begin() + i);
		break;
	}
	__String *strimg = __String::createWithFormat("img_%s.png", strFolder.c_str());
	std::string url = "q_right/";
	url.append(TypeRight_miniGame[indexTypeRight]);
	url.append("/");
	url.append(strFolder);
	url.append("/");
	urlFolder = url;
	url.append(strimg->getCString());
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	img->loadTexture(url);
#else
	img->loadTexture(strimg);
#endif	
	__String* numberBtn, *imgBtn;
	std::string urlImgBtn;
	urlImgBtn = urlFolder;
	int *ArrRandom = Utils::getArrRandomNumber(2);
	int numberFile;
	for (int i = 0; i < 2; i++)
	{
		numberFile = ArrRandom[i] + 1;
		numberBtn = __String::createWithFormat("btn_%d", i + 1);
		imgBtn = __String::createWithFormat("as_%d.png", numberFile);
		urlImgBtn.append(imgBtn->getCString());
		log("urlImgBtn:%s", urlImgBtn.c_str());
		auto btn = (Button*)(UIRight[indexTypeRight]->getChildByName(numberBtn->getCString()));
		if (numberFile == 1)
			btn->setTag(KBUTTONWIN_RIGHT_TAG);
		else
			btn->setTag(KBUTTONWRONG_RIGHT_TAG);
		btn->loadTextureNormal(urlImgBtn);
		btn->addTouchEventListener(this, toucheventselector(GamePlayScene::pressButtonWinRight));
		urlImgBtn = urlFolder;
	}
}
void GamePlayScene::matching_Paint()
{

}
void GamePlayScene::runner_Paint()
{

}
void GamePlayScene::pressButtonWinRight(Ref* pSender, TouchEventType type)
{
	if (type == TouchEventType::TOUCH_EVENT_ENDED)
	{
		auto btn = (Button*)pSender;
		Sprite* imgResult;
		if (btn->getTag() == KBUTTONWIN_RIGHT_TAG)
		{
			MessageBox("You Win!","Info");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
			imgResult = Sprite::create("gfx/GUI/img_correct.png");
			imgResult->setPosition(Vec2(UIRight[indexTypeRight]->getContentSize().width / 2, UIRight[indexTypeRight]->getContentSize().height / 2));
			imgResult->setTag(KIMGRESULT_RIGHT_TAG);
			UIRight[indexTypeRight]->addChild(imgResult);
#else
			imgResult = Sprite::create("img_correct.png");
			imgResult->setPosition(Vec2(UIRight[indexTypeRight]->getContentSize().width / 2, UIRight[indexTypeRight]->getContentSize().height / 2));
			imgResult->setTag(KIMGRESULT_RIGHT_TAG);
			UIRight[indexTypeRight]->addChild(imgResult);
#endif
			// Sum point
			PointSum += pointRight;
			__String *scoreTotal = __String::createWithFormat("%d", PointSum);
			txt_NumberScore->setString(scoreTotal->getCString());
		}
		else
		{
			MessageBox("You Wrong!", "Info");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
			imgResult = Sprite::create("gfx/GUI/img_incorrect.png");
			imgResult->setPosition(Vec2(UIRight[indexTypeRight]->getContentSize().width / 2, UIRight[indexTypeRight]->getContentSize().height / 2));
			imgResult->setTag(KIMGRESULT_RIGHT_TAG);
			UIRight[indexTypeRight]->addChild(imgResult);
#else
			imgResult = Sprite::create("img_incorrect.png");
			imgResult->setPosition(Vec2(UIRight[indexTypeRight]->getContentSize().width / 2, UIRight[indexTypeRight]->getContentSize().height / 2));
			imgResult->setTag(KIMGRESULT_RIGHT_TAG);
			UIRight[indexTypeRight]->addChild(imgResult);
#endif			 			
			// minus life
			s_LifeCount--;
			lifeArray[s_LifeCount]->setVisible(false);
			if (s_LifeCount > 0)
				playAnimTimerRight();
		}
		UIRight[indexTypeRight]->setEnabled(false);
		if (ArrGameFolderLeft.size() == 0)
		{
			UIRight[indexTypeRight]->setVisible(false);
			getRanMiniGameLeft();
		}
		else
		{
			this->scheduleOnce(schedule_selector(GamePlayScene::resetLayerRight), 0.2);
		}
	}
}
void GamePlayScene::resetLayerRight(float dt)
{
	pointRight = 100;
	txt_NumberScoreRigth->setString("100");
	auto imgResult = (Sprite*)(UIRight[indexTypeRight]->getChildByTag(KIMGRESULT_RIGHT_TAG));
	imgResult->removeFromParent();
	UIRight[indexTypeRight]->setEnabled(true);		 
	switch (indexTypeRight)
		{
		case COLOR:
			color_Paint();
			break;
		case CONCENTRATION:
			concentration_Paint();
			break;	
		case LEFTRIGHT:
			leftRight_Paint();
			break;	
		case MATCHING:
			matching_Paint();
			break;	
		case RUNNER:
			runner_Paint();
			break;
		default:
			break;
	}
}
void GamePlayScene::checkConcentration(Ref* pSender, TouchEventType type)
{
	if (type == TouchEventType::TOUCH_EVENT_ENDED)
	{
		auto btnTouched = (buttonApp*)pSender;
		arrConcent.push_back(btnTouched->getName());
		log("------name of button------:%s", btnTouched->getTextContentImg()->getString());
	}
}

										   